﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using TowerDefender.Graphics;
using TowerDefender.Entities;
using TowerDefender.Data;

namespace TowerDefender.Model
{

    public class Engine
    {
        private Window _window;
        private DispatcherTimer dispatcher;
        private int _tickCount = 0;

        public Engine(Window window)
        {
            _window = window;
             dispatcher = new DispatcherTimer();
            this.Configure();
        }

        private void Configure()
        {
            dispatcher.Interval = TimeSpan.FromMilliseconds(50);
            dispatcher.Tick += new EventHandler(dt_Tick);
            this.Tick +=new EventHandler(Engine_Tick);
        }

        void Engine_Tick(object sender, EventArgs e)
        {
            _tickCount++;
        }

        public int TickCount { get { return _tickCount; } }

        public void Start()
        {
            dispatcher.Start();

            string path = System.IO.Directory.GetCurrentDirectory() + "/Resources/Maps/Map1.xml";
            Map map = FileManager.LoadMap(path);

            Grid g = MapManager.CreateMap(_window, map);
            Peon p = new Peon(map, this, 1);
            PeonManager.CreatePeonGraphics(p, map, g);
            Tick(this, new EventArgs());

            Peon p2 = new Peon(map, this, 2);
            PeonManager.CreatePeonGraphics(p2, map, g);
        }
        public void Stop()
        {
            dispatcher.Stop();
        }
            
        public event EventHandler Tick;

        void dt_Tick(object sender, EventArgs e)
        {
            if (Tick != null)
            {
                Tick(this, e);
            }
        }
    }
}